package org.sy.lab.多线程基础._4_执行器._3_分离任务启动和结束的处理;

import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {

	public static void main(String[] args) {
		
		ThreadPoolExecutor executorService = (ThreadPoolExecutor) Executors.newCachedThreadPool();
		CompletionService<String> service = new ExecutorCompletionService<String>(executorService);
		
		Producer producer1 = new Producer("生产者1", service);
		Producer producer2 = new Producer("生产者2", service);
		Consumer consumer = new Consumer("消费者", service);
		
		Thread thread1 = new Thread(producer1);
		Thread thread2 = new Thread(producer2);
		Thread thread3 = new Thread(consumer);
		
		thread1.start();
		thread2.start();
		thread3.start();
		
		try {
			TimeUnit.SECONDS.sleep(5);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		executorService.shutdown();
		consumer.setEnd();
	}
}
